package com.matthicks.macrobot;

import java.io.Serializable;

public class Command implements Serializable {
	private static final long serialVersionUID = -5248799921192794566L;

	public String name;
	public String[] args;
	
	public Command() {
	}
	
	public Command(String s) {
		if (s.indexOf('(') == -1) {
			s += "()";
		}
		if (s.endsWith(";")) {
			s = s.substring(0, s.length() - 1);
		}
		name = s.substring(0, s.indexOf('('));
		String values = s.substring(s.indexOf('(') + 1, s.length() - 1);
		if (values.trim().length() > 0) {
			args = values.split(",");
			for (int i = 0; i < args.length; i++) {
				args[i] = args[i].trim();
				if ((args[i].startsWith("\"")) && (args[i].endsWith("\""))) {
					args[i] = args[i].substring(1, args[i].length() - 1);
				}
			}
		} else {
			args = new String[0];
		}
	}
	
	public Command(String name, String ... args) {
		this.name = name;
		this.args = args;
	}
	
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(name);
		b.append('(');
		for (int i = 0; i < args.length; i++) {
			if (i > 0) {
				b.append(',');
			}
			b.append(args[i]);
		}
		b.append(')');
		
		return b.toString();
	}
}